// plugin/components/hello-component.js
Component({
  properties: {
    items: {
      type: Array,
      value: [],
      observer(newVal, oldVal, changedPath) {
        this.setData({items: newVal})
      }
    }
  },

  data: {
    items: [],
    weeksCh:['日','一','二','三','四','五','六'],
    curYear: new Date().getFullYear(),
    curMonth: new Date().getMonth() + 1,
    monthList: [],
    curMonthDay: 0,
    curMonthLen: 30,
    isGray: false,
    checkList: []
  },

  ready: function() {
    this.getCurDayList();
  },

  methods: {
    canShowGray: function() {
      // 新旧时间戳对比
      const curPageDate = new Date(`${this.data.curYear}.${this.data.curMonth - 1}.1`).getTime();
      const nowYear = new Date().getFullYear();
      const nowMonth = new Date().getMonth();
      const nowDate = new Date(`${nowYear}.${nowMonth}.1`).getTime();
      return curPageDate < nowDate;
    },
    onPrev: function() {
      const curMonth = this.data.curMonth;
      if ( curMonth === 1) {
        this.setData({
          curYear: this.data.curYear - 1,
          curMonth: 12
        })
      } else {
        this.setData({
          curMonth: curMonth - 1
        })
      }
      this.getCurDayList();
    },
    onNext: function() {
      const curMonth = this.data.curMonth;
      if (curMonth === 12) {
        this.setData({
          curMonth: 1,
          curYear: this.data.curYear + 1
        })
      } else {
        this.setData({
          curMonth: curMonth + 1
        })
      }
      this.getCurDayList();
    },
    getCurrentDay: function() {

    },
    onCheckDate: function(value) {

    },
    getCurDayList: function() {
      const year = this.data.curYear;
      const month = this.data.curMonth;
      let monthList = [];
      const curMonthDay = new Date(`${year}.${month}.1`).getDay();
      const curMonthLen = this.getMonthLength(new Date(`${year}.${month}`));
      let prevMonthAll = this.getMonthLength(new Date(`${year}.${month - 1}`));
      const prevMonthLen = curMonthDay;
      for(let i = 0; i < prevMonthLen; i++) {
        monthList.unshift(prevMonthAll - i);
      };
      for(let i = 1; i <= curMonthLen; i++) {
        monthList.push(i)
      };
      for(let i = 1; i <= 7 + 7 - prevMonthLen; i++) {
        monthList.push(i)
      }
      this.setData({
        monthList: monthList,
        curMonthDay: curMonthDay,
        curMonthLen: curMonthLen,
        isGray: this.canShowGray()
      })
    },
    getMonthLength:(date = new Date()) => {
      const d = new Date(date);
      const y = d.getFullYear();
      const m = d.getMonth() - 0 + 1;
      return new Date(y, m, 0).getDate();
    }
  }
})
